package com.haotunet.app.goldtreasure.activity;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.view.WindowManager;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class BaseActivity extends FragmentActivity {
    public SystemBarTintManager tintManager;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        MIUISetStatusBarLightMode(getWindow(), true);
        tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.color_gray);// 通知栏所需颜色
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Log.e("lf", ActivityUtils.getRunningActivityName(this));
        if (!"MainActivity".equals(ActivityUtils.getRunningActivityName(this))) {
            MobclickAgent.onPageStart(ActivityUtils.getRunningActivityName(this));
        }
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (!"MainActivity".equals(ActivityUtils.getRunningActivityName(this))) {
            MobclickAgent.onPageEnd(ActivityUtils.getRunningActivityName(this));
        }
        MobclickAgent.onPause(this);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);// 状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);// 清除黑色字体
                }
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

    /**
     * 判断Activity是否可用
     * @return Activity是否可用
     */
    protected boolean isAvailable() {
        return !this.isFinishing() && !this.isDestroyed() && !this.isRestricted();
    }
}
